package mn.idax.exchange.otc.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import mn.idax.exchange.otc.domain.SimpleUserLanguage;
import mn.idax.exchange.otc.entity.JPushLog;
import mn.idax.exchange.otc.entity.OTCOrder;
import mn.idax.exchange.otc.entity.User;
import mn.idax.exchange.otc.enums.JiGuangPushEnum;
import mn.idax.exchange.otc.service.JiGuangService;
import mn.idax.exchange.otc.service.UserLanguageService;
import mn.idax.exchange.otc.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 * @Author: jack_huang
 * @Date: 2019/3/1 15:59
 */
@Service
public class JiGuangServiceImpl implements JiGuangService {

    private static final Logger logger = LoggerFactory.getLogger(JiGuangServiceImpl.class);

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private UserService userService;

    @Autowired
    private UserLanguageService userLanguageService;

    @Value("${spring.activemq.JPUSH_MESSAGE_QUEUE}")
    private String JPUSH_MESSAGE_QUEUE;

    @Override
    public void pushTemplate(OTCOrder order, String receiver, Integer receiverUserid, Boolean isBuyer, String language,JiGuangPushEnum jiGuangPushEnum, String... obj) {

        JPushLog jPushLog = new JPushLog();
        jPushLog.setBusiness(jiGuangPushEnum.getCode());
        jPushLog.setLanguage(language);
        jPushLog.setPlatform(new Short("1"));
        jPushLog.setNotification(new Short("1"));
        jPushLog.setAudience(receiver);
        jPushLog.setEmail(receiver);

        Map<String, Object> map = Maps.newHashMap();
        map.put("orderNum", order.getNumber());
        map.put("isBuyer", isBuyer.toString());
        map.put("data", obj);
        jPushLog.setParameter(JSON.toJSONString(map));

        //amq推送到极光服务
//        Destination destination = new ActiveMQQueue(JPUSH_MESSAGE_QUEUE);
//        jmsTemplate.convertAndSend(destination, JSON.toJSONString(jPushMessage));
        jmsTemplate.convertAndSend(JPUSH_MESSAGE_QUEUE, JSON.toJSONString(jPushLog));
        logger.info("pushTemplate run finish,amq send message {}",JSON.toJSONString(jPushLog));

    }

    /**
     * 极光推送给买卖方
     * @param otcOrder
     * @param jiGuangPushEnum
     */
    @Override
    public void pushToBuyerAndSeller(OTCOrder otcOrder,JiGuangPushEnum jiGuangPushEnum) {

        User buyer = userService.getUserById(otcOrder.getBuyer());
        User seller = userService.getUserById(otcOrder.getSeller());
        List<SimpleUserLanguage> languages = userLanguageService.getBuyerAndSellerSimpleLanguage(buyer.getId(), seller.getId());

        pushTemplate(otcOrder,buyer.getEmail(),buyer.getId(),true, languages.get(0).getLanguage(),jiGuangPushEnum,otcOrder.getNumber());
        pushTemplate(otcOrder,seller.getEmail(),seller.getId(),false, languages.get(1).getLanguage(),jiGuangPushEnum,otcOrder.getNumber());

    }
}
